//
//  DDifficultViewController.m
//  hellomui
//
//  Created by zhangtao on 15/10/30.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "DDifficultViewController.h"
#import "DDVideoView.h"
#import "LoginAndRegistViewController.h"
#import "Share.h"
@interface DDifficultViewController ()

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loading;

@property (weak, nonatomic) IBOutlet UIButton *playOrPause; //播放/暂停按钮
@property (weak, nonatomic) IBOutlet UIProgressView *progress;//播放进度
@property(nonatomic,weak)DDVideoView *ddv;
@property (weak, nonatomic) IBOutlet UIButton *shoucang;


@property(nonatomic,strong)UIButton *nextBtn;
@property(nonatomic,strong)UIButton *preBtn;

@end



@implementation DDifficultViewController
#pragma mark - 控制器视图方法

//-(void)viewWillAppear:(BOOL)animated{
////    self.tabBarController.tabBar.hidden = YES;
//    self.navigationController.navigationBar.hidden = NO;
//}
//

-(void)viewWillDisappear:(BOOL)animated{
//    [self.ddv.player removeObserver:self.ddv.player.currentItem forKeyPath:@"status"];
//
    [super viewWillDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self.ddv.player];
    [self.ddv.playerItem removeObserver:self.ddv forKeyPath:@"status"];
    [self.ddv.player pause];
    [self.ddv.player.currentItem cancelPendingSeeks];
    [self.ddv.player.currentItem.asset cancelLoading];
//
//    [self.ddv.player replaceCurrentItemWithPlayerItem:self.ddv.playerItem];
    NSLog(@"viewWill");
//    [self.ddv over];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];

     [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont systemFontOfSize:20.0],UITextAttributeFont,nil]];
    self.title = @"难题讲解";
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;// 字体的行间距
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    self.infDes.attributedText = [[NSAttributedString alloc] initWithString:ABOUT_US attributes:attributes];
    

//    UIColor * color = [UIColor whiteColor];
//     NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
// self.navigationController.navigationBar.titleTextAttributes = dict;
//
    //title大小以颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    self.navigationController.navigationBar.titleTextAttributes
//    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIFont fontWithName:@"难题讲解" size:30.0],UITextAttributeFont,nil]];
    
}


#pragma mark - 私有方法
-(void)setupUI{
    //创建播放器

    DDVideoView *dView = [[DDVideoView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), MAIN_WIDTH, MAIN_HEIGHT-self.infoView.frame.origin.y-self.navigationController.navigationBar.frame.size.height- 50)];
    dView.dificultVC = self;
    self.ddv = dView;
    
    dView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:dView];
    [self request];

    

}



-(void)request{
//    {
//    _loading.hidden = NO;
//    _loading.backgroundColor = [UIColor grayColor];
//    [_loading setTintColor:[UIColor grayColor]];
//    _loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//    [_loading startAnimating];
//    }
    
    NSString *path = @"secondClass/findVideoPlayer";      
   
    NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
//    NSMutableDictionary *dics = [NSMutableDictionary dictionaryWithObjectsAndKeys:_idValue,@"videoId",userId,@"userId",@"f1a881a4fd11a3c77c15c68d2de478e7",@"bd_sig", nil];
    NSDictionary *dicss = @{@"videoId":_idValue,@"userId":userId};

//    NSLog(@"~~~~~~~~%@",MD5);
    
    NSDictionary *dicVal = @{@"videoId":_idValue,@"userId":userId,@"bd_sig":[ToolObjc md5Str:dicss]};
    [[MBProgressController sharedInstance]showWithText:@"正在加载"];

    
    [[HttpConnctionManager sharedInstance]requestParam:dicVal andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        
        if ([[NSString stringWithFormat:@"%@",[succeedResult valueForKey:@"resCode"]] isEqualToString:@"0"]) {
            _bookId =  [[succeedResult valueForKey:@"resData"]stringWithFilted:@"bookId"];
            self.ddv.bookid = _bookId;
            _name.text = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"bookName"];
            _classes.text = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"grade"];
            _object.text = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"subject"];
            _infDes.text = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"videoProfile"];
            _videoId = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"videoUrl"];
            NSLog(@":::::%@",_videoId);
            NSUSER_DEF_NORSET(_videoId, @"VIDEOURL");
            NSLog(@"~~~~~~~~~~~~%@",_videoId);
            _videoid = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"videoId"];
            
            NSString *shouchang = [[succeedResult valueForKey:@"resData"]stringWithFilted:@"isCollection"];
            __isShoucang = shouchang;
            if ([__isShoucang isEqualToString:@"0"]) {
                [_shoucang setImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
            }else{
                [_shoucang setImage:[UIImage imageNamed:@"save_1.png"] forState:UIControlStateNormal];
            }

//            [self setupUI];

        }else{
            NSLog(@"xxxxxxxxx");
            UIStoryboard *storyBoard= [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            LoginAndRegistViewController *login = [storyBoard instantiateViewControllerWithIdentifier:@"loginAndRegist"];
            [self presentViewController:login animated:NO completion:nil];
        }
        [_loading stopAnimating];
        _loading.hidden = YES;
        
        NSLog(@"%@",succeedResult);
//
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        //
        [_loading stopAnimating];
        _loading.hidden = YES;
    }];
    [[MBProgressController sharedInstance]hide];

}

- (IBAction)shoucang:(id)sender {
    NSLog(@"shoucang");
    NSString *path = @"secondClass/videoCollection";
    
    NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
    NSDictionary *dic = @{@"videoId":_idValue,@"userId":userId};
    NSDictionary *dics = @{@"videoId":_idValue,@"userId":userId,@"bd_sig":[ToolObjc md5Str:dic]};
    NSLog(@"%@",dics);
    [[HttpConnctionManager sharedInstance]requestParam:dics andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        
        if ([__isShoucang isEqualToString:@"0"]) {
            __isShoucang = @"1";
            [_shoucang setImage:[UIImage imageNamed:@"save_1.png"] forState:UIControlStateNormal];
            [ToolObjc showMessage:@"收藏成功"];
            
        }else{
            __isShoucang = @"0";
            [_shoucang setImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
            [ToolObjc showMessage:@"取消收藏"];
            
        }
        
        //        if ([rescode isEqualToString:@"0"]) {
        //        }
        
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        
    }];

}


-(void)nextAntPre:(NSString *)sel{
}




- (IBAction)share:(id)sender {
    NSLog(@"~~~");
    [Share TencentShare];
}

@end
